package com.myapp.ruyixing.ui.shopmanager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.Model;
import com.myapp.ruyixing.ui.BaseActivity;
import com.myapp.ruyixing.ui.ChooseLocationActivity;
import com.myapp.ruyixing.ui.NearbyStoreActivity;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LocationUtils;
import com.myapp.ruyixing.utils.LoginHelper;

public class ShopEditActivity extends BaseActivity implements
		BDLocationListener {

	private TitleBar mTitleBar;
	private View mLoadingView;

	private EditText mEditText;

	private String mField;
	private String mContent;

	private LocationClient mLocationClient;
	private BDLocation mLocation;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				Model result = (Model) o;
				if (result != null && result.errno == 0) {
					Toast.makeText(ShopEditActivity.this, "修改成功",
							Toast.LENGTH_SHORT).show();
					hideLoading();
					Intent intent = new Intent();
					intent.putExtra("field", mField);
					intent.putExtra("content", mContent);
					setResult(RESULT_OK, intent);
					finish();
					return;
				} else {
					hideLoading();
					Toast.makeText(ShopEditActivity.this,
							"修改失败:" + result == null ? "" : result.msg,
							Toast.LENGTH_SHORT).show();
				}
			} catch (Exception e) {
				hideLoading();
				Toast.makeText(ShopEditActivity.this, "修改失败",
						Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			hideLoading();
			Toast.makeText(ShopEditActivity.this, "修改失败", Toast.LENGTH_SHORT)
					.show();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_shop_manager_shop_edit);

		initView();
	}

	private void initView() {
		mTitleBar = (TitleBar) findViewById(R.id.title_bar);
		mLoadingView = findViewById(R.id.my_loading);

		mEditText = (EditText) findViewById(R.id.edit);

		mTitleBar.setOnIconClickedListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		mField = getIntent().getStringExtra("field");

		((TextView) findViewById(R.id.label)).setText(getIntent()
				.getStringExtra("name") + "：");

		if ("address".equals(mField)) {
			View v = findViewById(R.id.locate);
			v.setVisibility(View.VISIBLE);
			v.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(ShopEditActivity.this,
							ChooseLocationActivity.class);
					startActivityForResult(intent, 888);
				}
			});

			mLocationClient = new LocationClient(this,
					LocationUtils.getDefaultOption());
			mLocationClient.registerLocationListener(this);
			mLocationClient.start();
			mLocationClient.requestLocation();
			showLoading();
		}

		findViewById(R.id.btn_commit).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContent = mEditText.getText().toString().trim();
				if (TextUtils.isEmpty(mContent)) {
					Toast.makeText(ShopEditActivity.this, "内容不能为空哦！",
							Toast.LENGTH_SHORT).show();
					mEditText.requestFocus();
					return;
				}
				showLoading();
				String longitude = "", latitude = "";
				if (mLocation != null) {
					longitude = "" + mLocation.getLongitude();
					latitude = "" + mLocation.getLatitude();
				}
				RYHttpClient.updateShop(ShopEditActivity.this,
						mHttpResponseHandler, LoginHelper.getInstance()
								.getShopId(), LoginHelper.getInstance()
								.getShopBduss(), mField, mContent, latitude,
						longitude);
			}
		});

	}

	@Override
	public void onReceiveLocation(BDLocation arg0) {
		if (arg0 != null && arg0.getLatitude() > 0.1
				&& arg0.getLongitude() > 0.1) {
			mLocation = arg0;
			hideLoading();
			mLocationClient.stop();
		}
	}

	@Override
	public void onReceivePoi(BDLocation arg0) {

	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		super.onActivityResult(arg0, arg1, arg2);

		if (arg0 == 100 && arg1 == RESULT_OK && arg2 != null) {
			String typeid = arg2
					.getStringExtra(NearbyStoreActivity.INTENT_KEY_TYPEID);
			String typeName = arg2
					.getStringExtra(NearbyStoreActivity.INTENT_KEY_TYPE_NAME);

			((TextView) findViewById(R.id.edit_channel)).setText(typeName);
		} else if (arg0 == 888 && arg1 == RESULT_OK && arg2 != null) {
			String address = arg2.getStringExtra("address");
			int lat = arg2.getIntExtra("lat", 0);
			int lon = arg2.getIntExtra("lon", 0);
			if (!TextUtils.isEmpty(address)) {
				mEditText.setText(address);
			}
			if (lat != 0 && lon != 0) {
				mLocationClient.stop();
				mLocation = new BDLocation();
				mLocation.setLongitude(((double) lon) / 1E6);
				mLocation.setLatitude(((double) lat) / 1E6);
			}
		}
	}

	private void showTitleBarLoading() {
		mTitleBar.showLoading(true);
	}

	private void hideTitleBarLoading() {
		mTitleBar.showLoading(false);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}
}
